package com.blb.community.controller;

import com.blb.community.utils.ResponseResult;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Author 帅帅
 * @Date 2022/7/6 11:03
 * @PackageName:com.blb.community.controller
 * @ClassName: UploadContoller
 * @Description: TODO
 * @Version 1.0
 */
@Api(tags = "图片上传")
@RestController
@RequestMapping
public class UploadController {
    @Value("${img.dir}")
    private String dir;

    @Autowired
    private FastFileStorageClient client;

    @PostMapping("/upload")
    @ApiOperation(value = "图片上传")
    public ResponseResult<String> upload(MultipartFile file) throws IOException {
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        StorePath storePath = client.uploadFile(file.getInputStream(), file.getSize(), extension, null);
        System.out.println(storePath);
        return ResponseResult.ok(dir + storePath.getFullPath());
    }
}
